package com.clob.dao;

import com.clob.exception.DBException;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DaoAssistant {

    /**
     * 获取列名
     * @param resultSet
     * @return
     */
     public List<String> columnLables(ResultSet resultSet){
        List<String> columnLables = new ArrayList<>();
        try {
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 0; i < metaData.getColumnCount(); i++) {
                String columnLabel = metaData.getColumnLabel(i + 1);
                columnLables.add(columnLabel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return columnLables;
    }

    /**
     * 为prepareStatement 设置参数
     * @param preparedStatement
     * @param objs
     */
    public void initParameters(PreparedStatement preparedStatement, Object ... objs){
        for (int i = 0; i < objs.length; i++) {
            try {
                preparedStatement.setObject(i + 1, objs[i]);
            } catch (SQLException e) {
                throw new DBException(this.getClass() + "设置参数错误");
            }
        }
    }

}
